<!--  -->
<template>
  <div class="save-folder">
    <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" @closed="close" :close-on-click-modal="false">
      <el-form :model="formInfo" v-loading="loading" :rules="rules" ref="refForm" label-width="100px">
        <el-form-item label="备份名称" prop="filename">
          <el-input v-model.trim="formInfo.filename" autocomplete="off" style="width: 100%;" placeholder="输入小于120个字符的备份名称" size="medium">
          </el-input>
        </el-form-item>
        <el-form-item label="备份描述" prop="description">
          <el-input size="medium" maxlength="500" type="textarea" :rows="4" v-model="formInfo.description" style="width: 100%;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close" size="small">取 消</el-button>
        <el-button type="primary" @click="save" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    resetDataTable: {
      type: Function
    },
    backupDisabled: {
      type: Boolean
    }
  },
  data () {
    var checkName = (rule, value, callback) => {
      let regx = /^[\u4e00-\u9fa50-9A-Za-z][\u4e00-\u9fa50-9A-Za-z@_.-]*$/
      if (value.trim() === '') {
        callback(new Error('备份名称不能都为空格！'))
      } else if (value > 120) {
        callback(new Error('备份名称不能超过120个字！'))
      } else if (!regx.test(value)) {
        callback(new Error('备份名称不能带特殊字符（除@_.-外，且@_.-不能作为开头）或空格！'))
      } else {
        callback()
      }
    }
    return {
      title: '添加备份',
      loading: false,
      dialogVisible: false,
      formInfo: {
        filename: '',
        description: '网站管理员手动备份'
      },
      rules: {
        filename: [
          { required: true, message: '请输入备份名称', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在1到120个字符', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    create () {
      this.dialogVisible = true
      // let year = new Date().getFullYear()
      // let month = new Date().getMonth() > 8 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1)
      // let day = new Date().getDate() > 9 ? new Date().getDate() : '0' + new Date().getDate()
      // this.formInfo.filename = '网站备份' + year + month + day
      this.$post('/host-backup-restore/default-backup-filename').then(response => {
        if (response) {
          this.formInfo.filename = response
        }
      })
    },
    close () {
      if (this.$refs['refForm']) {
        this.$refs['refForm'].clearValidate()
      }
      this.formInfo = {
        filename: '',
        description: '网站管理员手动备份'
      }
      this.dialogVisible = false
      this.loading = false
    },
    save () {
      if (this.backupDisabled) {
        this.$message.error('无权使用备份功能，请购买增值！')
        return
      }
      if (this.loading) {
        return
      }
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.formInfo.filename = this.formInfo.filename.trim()
          this.$post('/host-backup-restore/backup', this.formInfo).then(response => {
            this.loading = false
            this.close()
            this.resetDataTable(this.formInfo.filename)
          })
        }
        return false
      })
    }
  }
}
</script>

<style scoped>
.save-folder /deep/ .el-dialog__body {
  padding-bottom: 10px;
}
.save-folder /deep/ .el-form-item {
  margin-bottom: 20px;
}
</style>
